package com.net.oil.car.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.car.bean.CarSeriesBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：lch
 * 时间：2018/4/9 0009:下午 15:06
 * 邮箱：245850413@qq.com
 * 说明：
 */

public class CarSeriesAdapter extends RecyclerView.Adapter<CarSeriesAdapter.ViewHolder> {


    private Context context;
    private LayoutInflater inflater;
    private List<CarSeriesBean> list;

    private OnCarSeriesItemClickListener onCarSeriesItemClickListener;

    public CarSeriesAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        list = new ArrayList<>();
    }

    public void updateData(List<CarSeriesBean> list){
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public void setOnCarSeriesItemClickListener(OnCarSeriesItemClickListener onCarSeriesItemClickListener) {
        this.onCarSeriesItemClickListener = onCarSeriesItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.car_series_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final CarSeriesBean carSeriesBean = list.get(position);
        if (carSeriesBean != null){
            if (carSeriesBean.getName() != null){
                holder.carSeriesName.setText(carSeriesBean.getName());
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onCarSeriesItemClickListener.onClick(carSeriesBean.getId());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.car_series_name)
        TextView carSeriesName;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnCarSeriesItemClickListener{

        /**
         * 车系列表点击
         * @param seriesId  车系id
         */
        void onClick(int seriesId);
    }
}
